package com.promise.jokerdream.database.entity

import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.Index
import androidx.room.PrimaryKey
import kotlinx.serialization.Serializable

/**
 * Room 实体类 - 定时任务配置
 */
@Serializable
@Entity(
    tableName = "timer_task_config",
    foreignKeys = [
        ForeignKey(
            entity = UserConfigEntity::class,
            parentColumns = ["userId"],
            childColumns = ["userId"],
            onDelete = ForeignKey.CASCADE
        )
    ],
    indices = [Index(value = ["userId", "taskId"], unique = true)]
)
data class TimerTaskConfigEntity(
    @PrimaryKey(autoGenerate = true)
    val id: Long = 0,
    val userId: String,
    val taskId: String, // TimerTaskConfig 的 id
    val taskName: String,
    val startTime: String, // 格式：HH:mm
    val endTime: String, // 格式：HH:mm
    val isEnabled: Boolean = true,
    val lastExecutedDate: String? = null // 格式：yyyy-MM-dd
) {
    /**
     * 判断今天是否已执行
     * 游戏的一天是从早上6点到第二天早上6点
     */
    fun isTodayExecuted(): Boolean {
        if (lastExecutedDate == null) return false
        val now = java.time.LocalDateTime.now()
        val currentHour = now.hour
        
        // 获取游戏日期：如果当前时间在6点之前，则属于前一天
        val gameDate = if (currentHour < 6) {
            now.minusDays(1).toLocalDate()
        } else {
            now.toLocalDate()
        }
        
        return lastExecutedDate == gameDate.toString()
    }
}

